package de.herberlin.webapp.image;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ImageTag extends TagSupport {

	private int imageId=0;
	private String type=null;
	private String style=null;
	
	private transient Log logger=LogFactory.getLog(getClass());
	
	@Override
	public int doStartTag() throws JspException {

		HttpServletRequest req=(HttpServletRequest)pageContext.getRequest();
		ImageDescription imgDescr=ImageDescription.valueOf(type);
		
		StringBuffer buffer=new StringBuffer();
		buffer.append("<img border=\"0\" src=\"");
		buffer.append(req.getContextPath());
		buffer.append("/");
		buffer.append(imgDescr.name());
		buffer.append("_");
		buffer.append(imageId);
		buffer.append(".img\" ");
		
		buffer.append("width=\"");
		buffer.append(imgDescr.getWidth());
		buffer.append("\" ");
		
		buffer.append("height=\"");
		buffer.append(imgDescr.getHeight());
		buffer.append("\"");

		if (style!=null) {
			buffer.append("style=\"");
			buffer.append(style);
			buffer.append("\"");
		}
		
		buffer.append(">\n");
		
		try {
			pageContext.getOut().write(buffer.toString());
		} catch (IOException e) {
			logger.error(e,e);
		}
		
		return super.doStartTag();
	}


	public void setImageId(int id) {
		this.imageId = id;
	}


	public void setStyle(String style) {
		this.style = style;
	}


	public void setType(String type) {
		this.type = type;
	}

	
}
